<template>
  <!-- 自定义 Tabbar 容器 -->
  <view class="custom-tabbar">
    <!-- 遍历 tabList 生成 Tabbar 项 -->
    <view
      v-for="(item, index) in tabList"
      :key="index"
      class="tabbar-item"
      :class="{ 'tabbar-item-active': current === index }"
      @click="switchTab(item.pagePath)"
    >
      <view
        class="cart-count"
        v-if="cartCount !== 0 && item.pagePath === '/pages/cart/index'"
      >
        {{ cartCount }}
      </view>
      <view class="tabbar-item-icon">
        <image
          :src="`/static/tabbar/${
            current === index ? item.selectedIcon : item.icon
          }.png`"
          mode="widthFix"
        />
      </view>
      <text>{{ item.text }}</text>
    </view>
  </view>
</template>

<script>
// 定义路由映射关系，用于快速查找当前页面对应的索引
const TAB_ROUTES = {
  '/pages/home/index': 0,
  '/pages/category/index': 1,
  '/pages/cart/index': 2,
  '/pages/user/index': 3,
}
import { mapState } from 'vuex'
export default {
  name: 'customTabBar',
  props: {
    current: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      tabList: [
        {
          pagePath: '/pages/home/index',
          text: '首页',
          icon: 'home',
          selectedIcon: 'home-active',
        },
        {
          pagePath: '/pages/category/index',
          text: '商品分类',
          icon: 'category',
          selectedIcon: 'category-active',
        },
        {
          pagePath: '/pages/cart/index',
          text: '购物车',
          icon: 'cart',
          selectedIcon: 'cart-active',
        },
        {
          pagePath: '/pages/user/index',
          text: '我的',
          icon: 'user',
          selectedIcon: 'user-active',
        },
      ],
    }
  },
  computed: {
    ...mapState(['cartCount']),
  },
  mounted() {
    // console.log(this.cartCount, '购物车数量')
  },
  methods: {
    // 切换 tab 的方法
    switchTab(url) {
      uni.switchTab({
        url,
      })
    },
  },
  watch: {},
}
</script>

<style lang="scss" scoped>
/* Tabbar 容器样式 */
.custom-tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 110rpx;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #e7e7e7;
  border-top: 1rpx solid #e2e8f0;
  box-shadow: 0 0px 10rpx rgba(0, 0, 0, 0.05);
  z-index: 9999;
  padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
  /* 适配全面屏 */
}

/* Tabbar 项基础样式 */
.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 24rpx;
  color: #666;
  padding: 10rpx 0;
  transition: all 0.2s ease;
  position: relative;
  .cart-count {
    position: absolute;
    top: 0;
    right: 0;
    background-color: var(--main-color);
    color: #fff;
    border-radius: 50%;
    width: 30rpx;
    height: 30rpx;
    line-height: 30rpx;
    text-align: center;
  }
  .tabbar-item-icon {
    width: 40rpx;
    height: 40rpx;
    image {
      width: 100%;
    }
  }
}

/* 激活状态样式 */
.tabbar-item-active {
  color: #c9a063;
}

/* 图标样式 */
:deep(.tabbar-icon) {
  font-size: 48rpx !important;
  margin-bottom: 4rpx;
}

/* 激活状态图标样式 */
:deep(.tabbar-icon-active) {
  color: #c9a063 !important;
}

/* 文字样式 */
.tabbar-item text {
  margin-top: 6rpx;
  font-weight: 500;
}
</style>
